//
//  netServer.h
//
//  Created by fox on 12-11-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#ifndef netServer_h
#define netServer_h



#include "netSocket.h"

namespace FOXSDK
{
    typedef fvoid(*netServerHandler)(netSocket* s);
    typedef fvoid(*netServerSHandler)(netSocket* s , netMsgHead* h );
    
    class netServer : public io_service
    {
       
        
    public:
        
        friend class netSocket;
        
        netServer();
        virtual ~netServer();
        
        fvoid   SetAccept( netSocket* socket );
        fvoid   SetAddress( const fbyte* ip , fuint16 port );
        fvoid   SetHandler( netServerHandler enter , netServerHandler exit , netServerSHandler msg );
        
        fvoid   ServerRun();
        fvoid   ServerUpdate();
        
        fvoid   Start();
        fvoid   Stop();
        
        fvoid   Update();
        
        
    protected:
        
        netServerHandler OnEnter;
        netServerHandler OnExit;
        netServerSHandler OnMsg;
        
        fvoid   HandleStart();
        fvoid   HandleAccept( const boost::system::error_code& error , netSocket* socket );
        
        netSocket*      GetFreeSocket();
        
        
        SocketVector    mFreeSocket;
        SocketVector    mUsedSocket;
        SocketVector    mAcceptSocket;
        
        netSocket*  mSocket[ MAX_SOCKET ];
        
        tcp::endpoint   mServerAddr;
        tcp::acceptor   mAcceptor;
        
        thread  mServiceThread;
        mutex   mClientListMutex;
        
    };

}





#endif
